{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "jewish-therapy",
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "import pytz\n",
    "import datetime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "elect-thing",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1615468774.6171358\n"
     ]
    }
   ],
   "source": [
    "print(time.time())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "divided-favor",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "本地时间为 : time.struct_time(tm_year=2021, tm_mon=3, tm_mday=11, tm_hour=21, tm_min=21, tm_sec=5, tm_wday=3, tm_yday=70, tm_isdst=0)\n"
     ]
    }
   ],
   "source": [
    "localtime = time.localtime(time.time())\n",
    "print (\"本地时间为 :\", localtime)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "institutional-aerospace",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "本地时间为 : Thu Mar 11 21:21:28 2021\n"
     ]
    }
   ],
   "source": [
    "\n",
    "localtime = time.asctime( time.localtime(time.time()) )\n",
    "print (\"本地时间为 :\", localtime)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ideal-cable",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2021-03-11 21:22:37\n",
      "Thu Mar 11 21:22:37 2021\n",
      "1459175064.0\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# 格式化成2016-03-20 11:45:39形式\n",
    "print (time.strftime(\"%Y-%m-%d %H:%M:%S\", time.localtime()))\n",
    "\n",
    "# 格式化成Sat Mar 28 22:24:24 2016形式\n",
    "print (time.strftime(\"%a %b %d %H:%M:%S %Y\", time.localtime()))\n",
    " \n",
    "# 将格式字符串转换为时间戳\n",
    "a = \"Sat Mar 28 22:24:24 2016\"\n",
    "print (time.mktime(time.strptime(a,\"%a %b %d %H:%M:%S %Y %Z\")))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "negative-consciousness",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "以下输出2016年1月份的日历:\n",
      "    January 2016\n",
      "Mo Tu We Th Fr Sa Su\n",
      "             1  2  3\n",
      " 4  5  6  7  8  9 10\n",
      "11 12 13 14 15 16 17\n",
      "18 19 20 21 22 23 24\n",
      "25 26 27 28 29 30 31\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import calendar\n",
    "\n",
    "cal = calendar.month(2016, 1)\n",
    "print (\"以下输出2016年1月份的日历:\")\n",
    "print (cal)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "excessive-story",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.537946495"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time.perf_counter()  # 返回系统运行时间\n",
    "time.process_time()  # 返回进程运行时间"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "waiting-cover",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-----------执行开始----------\n",
      "100%[**************************************************->]5.02s\n",
      "-----------执行结果----------\n"
     ]
    }
   ],
   "source": [
    "scale = 50 \n",
    "\n",
    "print(\"执行开始\".center(scale//2,\"-\"))  # .center() 控制输出的样式，宽度为 25//2，即 22，汉字居中，两侧填充 -\n",
    "\n",
    "start = time.perf_counter() # 调用一次 perf_counter()，从计算机系统里随机选一个时间点A，计算其距离当前时间点B1有多少秒。当第二次调用该函数时，默认从第一次调用的时间点A算起，距离当前时间点B2有多少秒。两个函数取差，即实现从时间点B1到B2的计时功能。\n",
    "for i in range(scale+1):   \n",
    "    a = '*' * i             # i 个长度的 * 符号\n",
    "    b = '.' * (scale-i)  # scale-i） 个长度的 . 符号。符号 * 和 . 总长度为50 \n",
    "    c = (i/scale)*100  # 显示当前进度，百分之多少\n",
    "    dur = time.perf_counter() - start    # 计时，计算进度条走到某一百分比的用时\n",
    "    print(\"\\r{:^3.0f}%[{}->{}]{:.2f}s\".format(c,a,b,dur),end='')  # \\r用来在每次输出完成后，将光标移至行首，这样保证进度条始终在同一行输出，即在一行不断刷新的效果；{:^3.0f}，输出格式为居中，占3位，小数点后0位，浮点型数，对应输出的数为c；{}，对应输出的数为a；{}，对应输出的数为b；{:.2f}，输出有两位小数的浮点数，对应输出的数为dur；end=''，用来保证不换行，不加这句默认换行。\n",
    "    time.sleep(0.1)     # 在输出下一个百分之几的进度前，停止0.1秒\n",
    "print(\"\\n\"+\"执行结果\".center(scale//2,'-'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "statistical-blues",
   "metadata": {},
   "outputs": [],
   "source": [
    "localtime = time.localtime(time.time())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "constitutional-usage",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "21"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "localtime.tm_hour"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "passive-threshold",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['America/New_York', 'America/Detroit', 'America/Kentucky/Louisville', 'America/Kentucky/Monticello', 'America/Indiana/Indianapolis', 'America/Indiana/Vincennes', 'America/Indiana/Winamac', 'America/Indiana/Marengo', 'America/Indiana/Petersburg', 'America/Indiana/Vevay', 'America/Chicago', 'America/Indiana/Tell_City', 'America/Indiana/Knox', 'America/Menominee', 'America/North_Dakota/Center', 'America/North_Dakota/New_Salem', 'America/North_Dakota/Beulah', 'America/Denver', 'America/Boise', 'America/Phoenix', 'America/Los_Angeles', 'America/Anchorage', 'America/Juneau', 'America/Sitka', 'America/Metlakatla', 'America/Yakutat', 'America/Nome', 'America/Adak', 'Pacific/Honolulu']\n"
     ]
    }
   ],
   "source": [
    "print(pytz.country_timezones('us'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "optional-initial",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2021-03-11 08:33:18\n"
     ]
    }
   ],
   "source": [
    "tz = pytz.timezone('America/New_York')\n",
    "a = datetime.datetime.now(tz).strftime(\"%Y-%m-%d %H:%M:%S\")\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "general-disposal",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2021-03-11 08:35\n"
     ]
    }
   ],
   "source": [
    "b=time.mktime(time.strptime(a,'%Y-%m-%d %H:%M:%S'))+int(2)*60\n",
    "print(time.strftime(\"%Y-%m-%d %H:%M\",time.localtime(b)))\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "divine-prairie",
   "metadata": {},
   "outputs": [],
   "source": [
    "# str转时间格式：\n",
    "dd = '20190317'\n",
    "dd = datetime.datetime.strptime(dd, \"%Y%m%d\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
